import { updatacardTableData } from 'ssccommon/utils/changePageData';

/**
 * 更新表体数据
 * @param {*} props
 * @param {*} bodysData
 */
function updateBodyData(props, bodysData) {
    window.presetVar.bodys.forEach((bodyCode) =>{
        bodysData[bodyCode] && props.cardTable.setTableData(bodyCode, bodysData[bodyCode])
    })
}

/**
 *
 * @param {*} rate_category 当前汇率类型
 * @param {*} windowTypes 所有汇率类型
 * @returns Y：五种类型之一，N：非五种类型，-1：空
 */
 function getZzhllxValue(rate_category = '',windowTypes){
    if(rate_category.display){
        let curType = windowTypes.find((item) => {
            return item.display === rate_category.display
        })
        let value = curType.value
        if(value == '0' || value == '1' || value == '2' || value == '3' || value == '4') return 'Y'
        return 'N'
    } else {
        return -1
    }
}

/**
 * 汇率适配逻辑
 * @param {*} currentrow_bzbm:当前币种，
 * @param {*} rate_category：汇率类型，
 * @param {*} type: 'head',表单，'body‘，表体，
 * @param {*} props:props
 * @param {*} moduleId:当前页签
 * @param {*} rowid:当前行
 * @returns
 */
 function zzhllxLogic(currentrow_bzbm,rate_category,type,props,bbhl = 'bbhl',moduleId,rowid){
    //  借款单中本币汇率字段：是org_currinfo不是bbhl，共用方法中 使用org_currinfo为本币汇率
    let zzbwb = window.presetVar.zzbwb //组织币种
    let ratecontorl = window.presetVar.ratecontorl   // 集团参数NC004 N 不允许修改，Y 允许修改
    let zzhllxValue = getZzhllxValue(rate_category,window.presetVar.zzhllxs)  // Y:五种中的一种，N:非五种，-1：空
    let editKeys = []
    let unEditKeys = []
    if(currentrow_bzbm && currentrow_bzbm.value == zzbwb){
        unEditKeys = [bbhl,'zzhllx']
        editKeys = ['zzhllyrq']
        if(zzhllxValue < 0){
            unEditKeys = ['zzhllyrq',bbhl,'zzhllx']
            editKeys = []
            type == 'head' ? props.form.setFormItemsValue(presetVar.head.head1,{'zzhllyrq':{display: null, value: null}}) : props.cardTable.setValByKeyAndRowId(moduleId, rowid, 'zzhllyrq',{value:null, display: null}) //清空组织汇率来源日期
        }
    } else if( zzhllxValue =='Y' && ratecontorl == 'N'){  //缺少对应5种组织汇率类型及集团参数判断
        unEditKeys = [bbhl]
        editKeys = ['zzhllyrq','zzhllx']
    }  else if(zzhllxValue < 0){
        unEditKeys = ['zzhllyrq']
        editKeys = [bbhl,'zzhllx']
        type == 'head' ? props.form.setFormItemsValue(presetVar.head.head1,{'zzhllyrq':{display: null, value: null}}) : props.cardTable.setValByKeyAndRowId(moduleId, rowid, 'zzhllyrq',{value:null, display: null}) //清空组织汇率来源日期
    } else{
        unEditKeys = []
        editKeys = [bbhl,'zzhllyrq','zzhllx']
    }
    if(type == 'head'){
        editKeys.forEach((item) => {
            props.form.setFormItemsDisabled('head',{[item]: false});
        })
        unEditKeys.forEach((item) => {
            props.form.setFormItemsDisabled('head',{[item]: true});
        })
    } else{
        props.cardTable.setEditableByRowId(moduleId, rowid, editKeys, true)
        props.cardTable.setEditableByRowId(moduleId, rowid, unEditKeys, false)
    }

}

function  delete3status(data){
    if(data.bodys){
        for(let bodyName in data.bodys){
            let filterrow =[];
            if(data.bodys[bodyName].rows){
                data.bodys[bodyName].rows.map((one)=>{
                    if(one.status!='3'){
                        filterrow[filterrow.length]=one;
                    }
                })
            }
            data.bodys[bodyName].rows = filterrow;
        }
    }
}

function setAllCardTableEditableKeys(props, data={}, changeKey) {
    let validBodyArray = [];
    window.presetVar.bodys.forEach((one)=>{
        if (data.bodys[one]){
            let temp = {
                "tablecode" : one,
                "tabledata" : data.bodys[one]
            }
            validBodyArray.push(temp);
        }
    })

    //拉单生成的报销单明细需要控制收款对象相关字段的可编辑性
    validBodyArray.forEach(validBody => {
        let tablecode = validBody.tablecode;
        let rows = props.cardTable.getAllRows(tablecode)
        if (rows){
            rows.forEach((row, rowIndex) => {
                //申请单表头没有收款对象,这块的逻辑就不成立
                // let currentrow_paytarget = props.form.getFormItemsValue("head", ['paytarget']) == null ? "" : props.form.getFormItemsValue("head", ['paytarget'])[0]//收款对象
                // if (currentrow_paytarget && props.form.getFormItemsVisible('head', 'paytarget')){
                //     let currentrow_paytargetvalue = currentrow_paytarget.value
                //     //控制表体收款对象相关字段的可编辑性
                //     let editablekeys = []
                //     let uneditablekeys = []
                //     if ("0" == currentrow_paytargetvalue){
                //         editablekeys = ['receiver','skyhzh']
                //         uneditablekeys = ['pk_supplier','pk_supplier_v','pk_customer','pk_customer_v','custaccount']
                //     }else if("1" == currentrow_paytargetvalue){
                //         editablekeys = ['pk_supplier','pk_supplier_v','custaccount']
                //         uneditablekeys = ['receiver','skyhzh','pk_customer','pk_customer_v']
                //     }else if("2" == currentrow_paytargetvalue){
                //         editablekeys = ['pk_customer','pk_customer_v','custaccount']
                //         uneditablekeys = ['receiver','skyhzh','pk_supplier','pk_supplier_v']
                //     }
                //     props.cardTable.setEditableByRowId(tablecode, row.rowid, editablekeys, true)
                //     props.cardTable.setEditableByRowId(tablecode, row.rowid, uneditablekeys, false)
                // }
                const keys = ['pk_supplier','pk_supplier_v','pk_customer','pk_customer_v']
                const default_values = {}
                keys.forEach(key => {
                    const value = props.form.getFormItemsValue('head', key)
                    default_values[key] = value.value ? value : {display:'', value: ''}
                })
                if( (changeKey === 'pk_supplier' && default_values[changeKey].value) ||  (changeKey === 'pk_supplier_v' && default_values[changeKey].value) ) {
                    default_values['pk_customer'] = {display: '', value: ''}
                    default_values['pk_customer_v'] = {display: '', value: ''}
                } else if((changeKey === 'pk_customer' && default_values[changeKey].value) ||  (changeKey === 'pk_customer_v' && default_values[changeKey].value)) {
                    default_values['pk_supplier'] = {display: '', value: ''}
                    default_values['pk_supplier_v'] = {display: '', value: ''}
                }
                props.cardTable.setValByKeysAndIndex(tablecode, rowIndex, default_values)
                props.form.setFormItemsValue('head', default_values)

                // 表体数据初始化时需设置汇率相关字段可编辑性
                let currentrow_bzbm = props.cardTable.getValByKeyAndRowId(tablecode, row.rowid, 'bzbm')
                console.log('ddd',currentrow_bzbm)
                let rate_category = props.cardTable.getValByKeyAndRowId(tablecode, row.rowid, 'zzhllx.rate_category')
                zzhllxLogic(currentrow_bzbm,rate_category,'body',props,'bbhl',tablecode,row.rowid)
            })
        }
        
    })
}

export {
    updateBodyData,
    getZzhllxValue,
    zzhllxLogic,
    delete3status,
    setAllCardTableEditableKeys
}
